package net.adetech.dcweb.core.${DBEntity.packageName}.rest;

import net.adetech.dcweb.core.${DBEntity.packageName}.entity.${DBEntity.pascalName};
import net.adetech.dcweb.core.${DBEntity.packageName}.service.I${DBEntity.pascalName}Service;
import net.adetech.utils.UtilsCommon;
import net.adetech.utils.spring.PageBean;
import net.adetech.web.utils.spring.BaseController;
import net.adetech.web.utils.UtilsUI;
import net.adetech.web.utils.UtilsWeb;
import net.adetech.utils.UtilsCondition;
import org.springframework.ui.Model;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.List;

/**
* Created by auto on 2016/10/21.
*/
@RestController
public class ${DBEntity.pascalName}RestController extends BaseController {

private final Logger logger = LoggerFactory.getLogger(${DBEntity.pascalName}RestController.class);

@Autowired
private I${DBEntity.pascalName}Service i${DBEntity.pascalName}Service;

@PostMapping(value="/${DBEntity.system}/${DBEntity.module}",produces = "application/json")
public String add(HttpServletRequest request, HttpServletResponse response) {
${DBEntity.pascalName} entity = this.wrapper(${DBEntity.pascalName}.class,request);
entity = i${DBEntity.pascalName}Service.save(entity);
response.setStatus(HttpServletResponse.SC_CREATED);
opLog(request,logger,"增加","");
return UtilsUI.data(entity);
}

@GetMapping(value="/${DBEntity.system}/${DBEntity.module}/{id}", produces = "application/json")
public String detail(@PathVariable Long id,HttpServletResponse response,HttpServletRequest request) {
${DBEntity.pascalName} entity = i${DBEntity.pascalName}Service.find(id);
if (null == entity) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
return null;
}
opLog(request,logger,"查看","");
return JSONObject.toJSONString(entity);
}

@PutMapping(value="/${DBEntity.system}/${DBEntity.module}/{id}", produces = "application/json")
public void update(HttpServletRequest request,@PathVariable Long id, HttpServletResponse response) {
${DBEntity.pascalName} entity = this.wrapper(${DBEntity.pascalName}.class,request);
entity.setId(id);
int rs = i${DBEntity.pascalName}Service.update(entity);
opLog(request,logger,"修改","");
if (rs > 0) {
response.setStatus(HttpServletResponse.SC_CREATED);
} else {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
}
}

@DeleteMapping(value="/${DBEntity.system}/${DBEntity.module}", produces = "application/json")
public String delete(HttpServletRequest request, HttpServletResponse response) {
String idsStr = UtilsWeb.getString(request,"ids");
if ("".equals(idsStr)) {
return UtilsUI.tip("请传入ids参数");
}
String [] ids = idsStr.split(",");
if (null != ids && ids.length > 0) {
List
<Long> idsLong = new ArrayList<>(0);
    for (int i = 0; i < ids.length; i++) {
    idsLong.add(UtilsCommon.getLong(ids[i]));
    }
    i${DBEntity.pascalName}Service.delete(idsLong);
    }
    opLog(request,logger,"删除","");
    response.setStatus(HttpServletResponse.SC_NO_CONTENT);
    return null;
    }

    @GetMapping(value="/${DBEntity.system}/${DBEntity.module}s", produces = "application/json")
    public String page(HttpServletRequest request) {
    Integer nowPage = UtilsWeb.getInteger(request, "nowPage");
    Integer pageSize = UtilsWeb.getInteger(request, "pageSize");
    if(null==nowPage||nowPage<=0){
    return UtilsUI.tip("未传入nowPage参数，或者nowPage参数错误！");
    }
    if(null==pageSize||pageSize<=0){
    return UtilsUI.tip("请传入pageSize参数，或者pageSize参数错误！");
    }
    PageBean<${DBEntity.pascalName}> pageBean = i${DBEntity.pascalName}Service.page(nowPage,
    pageSize,UtilsCondition.getConditionEntity(request.getParameterMap()));
    opLog(request,logger,"分页查看","");
    return UtilsUI.datagrid(pageBean);
    }
    }